package com.leyou.item.servcie.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.servcie.BrandService;
import com.leyou.item.servcie.CategoryBrandService;
import com.leyou.pojo.dto.BrandDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService{
    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public List<BrandDto> queryBrandByCategoryId(Long categoryId) {
        //根据分类id查询品牌id
        List<CategoryBrand> categoryBrandList = categoryBrandService.query().eq("category_id", categoryId).list();
        if(CollectionUtils.isEmpty(categoryBrandList)){
            throw new LyException("查询不到类别",400);
        }
        List<Long> ids = categoryBrandList.stream().map(CategoryBrand::getBrandId).collect(Collectors.toList());
        //根据品牌id查询品牌
        List<Brand> brandList = listByIds(ids);
        //转换成DTO
        List<BrandDto> brandDtoList = brandList.stream().map(BrandDto::new).collect(Collectors.toList());
        return brandDtoList;
    }

    @Override
    public PageDTO<BrandDto> queryBrandByPage(String key, Integer page, Integer rows) {
    //健壮性判断
         page = Math.max(1, page);
         rows = Math.max(1,rows);
        //判断是否不是空白
        boolean b = StringUtils.isNotBlank(key);
        Page<Brand> brandPage = query().like(b,"name", key)
                .or()
                .eq(b,"letter", key)
                .page(new Page<Brand>(page,rows));
        PageDTO<BrandDto> brandDtoPageDTO =
                new PageDTO<>(brandPage.getTotal(), brandPage.getPages(), brandPage.getRecords().stream().map(BrandDto::new).collect(Collectors.toList()));
        return brandDtoPageDTO;
    }

    @Override
    @Transactional
    public void saveBrand(BrandDto brandDto) {
        //转换成brand
        Brand brand = brandDto.toEntity(Brand.class);
        //先增加品牌表
        boolean save = save(brand);
        if(save==false){
            throw new LyException("增加品牌失败",500);
        }
        //在增加中间表
        List<Long> categoryIds = brandDto.getCategoryIds();
        //判断分类id是否为空
        if(CollectionUtils.isEmpty(categoryIds)){
            throw new LyException("传递的参数有误",400);
        }
        List<CategoryBrand> list = categoryIds.stream().map(categoryId -> CategoryBrand.of(categoryId, brand.getId())).collect(Collectors.toList());
        //批量 增加
        boolean b = categoryBrandService.saveBatch(list);
        if(b==false){
            throw  new LyException("添加品牌种类表失败",500);
        }
    }

    @Override
    @Transactional
    public void updateBrand(BrandDto brandDto) {
        //转换成brand
        Brand brand = brandDto.toEntity(Brand.class);
        //修改BRAND
        boolean b = updateById(brand);
        if(b==false){
            throw new LyException("修改品牌失败",500);
        }
        //查询分类id
        List<Long> categoryIds = brandDto.getCategoryIds();
        //先删除中间表，在增加
        categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",brand.getId()));
        //中间表添加
        List<CategoryBrand> list = categoryIds.stream().map(categoryId -> CategoryBrand.of(categoryId, brand.getId())).collect(Collectors.toList());
        //批量 增加
        boolean b1 = categoryBrandService.saveBatch(list);
        if(b1==false){
            throw  new LyException("添加品牌种类表失败",500);
        }
    }

    @Override
    @Transactional
    public void deleteBrand(Long brandId) {
        //先删除品牌表
        boolean b = removeById(brandId);
        if(b==false){
            throw new LyException("删除品牌表异常",500);
        }
        //再删除中间表
        boolean b1 = categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", brandId));
        if(b1==false){
            throw  new LyException("删除中间表异常",500);
        }
    }
}
